﻿using System;using ERP_RORZE_SYSTEM;
using System.Collections.Generic;
using System.Data;using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Product.Base
{
    public class ExecuteSQL
    {
        INISettings inisetting = new INISettings();
        private SqlConnection conn;
        private DataTable dTable;
        public DataTable DTable { get; set; }
        private SqlDataAdapter dAdapter;
        public SqlDataAdapter DAdapter { get; set; }

        public SqlConnection GetCon()
        {

            return new SqlConnection(inisetting.SQLConnectionString);
        }
        public DataTable DoStoreProcedure(string sql, bool setDataProperties)
        {       
            conn = GetCon();
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            if (setDataProperties)
            {
                dAdapter = new SqlDataAdapter();
                dAdapter.SelectCommand = cmd;
                dTable = new DataTable();
                dAdapter.Fill(dTable);
                return dTable;
            }
            else
            {
                SqlDataAdapter dataAdap = new SqlDataAdapter(sql, conn);
                DataTable dataTable = new DataTable();
                dataAdap.Fill(dataTable);
                return dataTable;
            }
        }

        public DataTable DoStoreGetByID(string sql,SqlParameter[] parameters, bool setDataProperties)
        {
            conn = GetCon();
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            if (setDataProperties)
            {
                foreach (
                SqlParameter sqlParameter in parameters)
                {
                    cmd.Parameters.Add(sqlParameter);
                }
                dAdapter = new SqlDataAdapter();
                dAdapter.SelectCommand = cmd;
                dTable = new DataTable();
                dAdapter.Fill(dTable);
                return dTable;
            }
            else
            {
                SqlDataAdapter dataAdap = new SqlDataAdapter(sql, conn);
                DataTable dataTable = new DataTable();
                dataAdap.Fill(dataTable);
                return dataTable;
            }
        }
        public void DoInsert(string sql, SqlParameter[] parameters, bool setDataProperties)
        {
            conn = GetCon();
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (
                SqlParameter sqlParameter in parameters)
            {
                cmd.Parameters.Add(sqlParameter);
            }
            
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        public void DoDelete(string sql, SqlParameter[] parameters, bool setDataProperties)
        {
            conn = GetCon();
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (
                SqlParameter sqlParameter in parameters)
            {
                cmd.Parameters.Add(sqlParameter);
            }
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        public void DoUpdate(string sql, SqlParameter[] parameters, bool setDataProperties)
        {
            try
            {
                conn = GetCon();
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                foreach (
                    SqlParameter sqlParameter in parameters)
                {
                    cmd.Parameters.Add(sqlParameter);
                }

                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show("" + e);
            }
        }
        public static SqlParameter CreateParameter(string parameterName, SqlDbType type, object value)
        {
            SqlParameter parameter = new SqlParameter();

            try
            {
                parameter.ParameterName = parameterName;
                parameter.SqlDbType = type;
                parameter.Value = value;

                return parameter;
            }
            catch (Exception e)
            {
                MessageBox.Show("Error:" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }

        }
        public DataTable GetAllTable(string sql, bool setDataProperties)
        {
            conn = GetCon();
            conn.Open();
            if (setDataProperties)
            {
                dAdapter = new SqlDataAdapter(sql, conn);
                dTable = new DataTable();
                dAdapter.Fill(dTable);
                return dTable;
            }
            else
            {
                SqlDataAdapter dataAdap = new SqlDataAdapter(sql, conn);
                DataTable dataTable = new DataTable();
                dataAdap.Fill(dataTable);
                return dataTable;
            }
        }
        public void DoStore(string sql, bool setDataProperties)
        {
            try
            {
                conn = GetCon();
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show("" + e);
            }
        }
    }
}
